/**
 * 每周期
 */
function everyTime(dom) {
    switch (dom.name) {
        case "second":
            setCronStr(1, "*");
            return;
        case "min":
            setCronStr(2, "*");
            return;
        case "hour":
            setCronStr(3, "*");
            return;
        case "day":
            setCronStr(4, "*");
            return;
        case "mouth":
            setCronStr(5, "*");
            return;
        case "week":
            setCronStr(6, "*");
            return;
    }
}

function setCronStr4Name(name, val) {
    switch (name) {
        case "second":
            setCronStr(1, val);
            return;
        case "min":
            setCronStr(2, val);
            return;
        case "hour":
            setCronStr(3, val);
            return;
        case "day":
            setCronStr(4, val);
            return;
        case "mouth":
            setCronStr(5, val);
            return;
        case "week":
            setCronStr(6, val);
            return;
    }
}

/**
 *  i:表示那个span
 */
function unAppoint(i) {
    var val = "?";
    if (name == "year")
        val = "";
    setCronStr(i, val)
}

/**
 * 设置 第i个 span 的值
 * @param i
 * @param val
 */
function setCronStr(i, val) {
    $("#cron_str span:nth-child(" + i + ")").html(val);
    getnextTime();
}

function getnextTime() {
    let txt = $("#cron_str").text();
    txt = txt.replace(/\s+/g, " ").trim();
    console.log(txt.trim());
    // 设置最近五次运行时间

    // $.ajax({
    //     type: 'get',
    //     url: "http://localhost/am/monitor/cron/next",
    //     dataType: "json",
    //     data: {
    //         "cron": encodeURIComponent(txt)
    //     },
    //     success: function (data) {
    //         console.log(data);
    //     }
    // });



}

/**
 * 周期
 */
function cycle(dom, name) {
    if (!name) {
        name = dom.name;
    }
    console.log(name);
    var ns = $(dom).parent().find(".num");
    var start = ns.eq(0).val();
    var end = ns.eq(1).val();
    setCronStr4Name(name, start + "-" + end)
}


/**
 * 从开始
 */
function startOn(dom, name) {
    if (!name) {
        name = dom.name;
    }
    var ns = $(dom).parent().find(".num");
    var start = ns.eq(0).val();
    var end = ns.eq(1).val();
    setCronStr4Name(name, start + "/" + end)
}

function lastDay(dom) {
    setCronStr4Name('day', "L")
}

function weekOfDay(dom) {
    var ns = $(dom).parent().find(".num");
    var start = ns.eq(0).val();
    var end = ns.eq(1).val();
    setCronStr4Name('week', start + "#" + end)

}

function lastWeek(dom) {
    var ns = $(dom).parent().find(".num");
    var start = ns.eq(0).val();
    setCronStr4Name('week', start + "L")
    setCronStr4Name('day', "?")
}

function workDay(dom) {
    var ns = $(dom).parent().find(".num");
    var start = ns.eq(0).val();
    setCronStr4Name('day', start + "W")
}

function sencond_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'second', 59);
        });
    }
}

function minute_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'min', 59);
        });
    }
}

function hour_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'hour', 59);
        });
    }
}

function day_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'day', 59);
        });
    }
}

function mouth_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'mouth', 59);
        });
    }
}

function week_appoint(input, class_name) {
    if (input.checked) {
        $("." + class_name).click(function () {
            appoint(class_name, 'week', 59);
        });
    }
}

function appoint(class_name, name, max) {
    let vals = [];
    $("." + class_name + ":checked").each(function (a, b) {
        vals.push(b.value);
    });
    var val = "?";
    if (vals.length > 0) {
        console.log(vals);
        if (vals.length < max) {
            val = vals.join(",");
        } else if (vals.length == max) {
            val = "*";
        }
    }
    setCronStr4Name(name, val)
}